<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%
String usernameSC = (String) session.getAttribute("session_name");
String idSC = (String) session.getAttribute("id");
String typeSC = (String) session.getAttribute("type");
if(usernameSC != null && !(typeSC.equals("student"))) {
	String driver ="com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/projects";
	String username = "ia";
	String password = "123";
	String name;
	String col = "";
	String data = "";
	String fontStyle = "";
	String fontSize = "";
	String[] events = new String[4];
	int index= 0;
	SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss a");
	SimpleDateFormat day = new SimpleDateFormat("MM/dd/yyyy");
	events[2]= day.format(new Date());
	events[3]= time.format(new Date());
	
	Enumeration project = request.getParameterNames();

	
	while(project.hasMoreElements())
	{
		name = (String)project.nextElement();
	
		if(!(name.equals("fColor") || name.equals("fStyle") || name.equals("fSize")))
		{
			col += name+",";
			data += "'"+request.getParameter(name)+"',";
		}
	
	}
	
	
	try
	{
		Class.forName(driver).newInstance();
		Connection connection = DriverManager.getConnection(url, username, password);
		Statement statement = connection.createStatement();
		
		ResultSet rsS = statement.executeQuery("select ref_id from user_log_in where id ="+ idSC);
		rsS.next();
		int prof_id = rsS.getInt(1);
				
		col += "Status,Creation_Date,Last_Modification_Date,prof_id";
		data += "'pend','"+events[2]+" "+events[3]+"','"+events[2]+" "+events[3]+"',"+prof_id;
		
		
		String query="INSERT INTO projects ("+col+") values("+data+")";
		statement.executeUpdate(query);
		
		events[0]="Professor: (" + usernameSC + ") add new project";
		events[1] = "Insertion process is successful";
//--------------------------------------------------------------
//----------- add events----------------------------------------
//--------------------------------------------------------------
	ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM events");
		rs.next();
	int counter  = rs.getInt(1);
	
	if(counter > 0)
	{
		ResultSet rs1=statement.executeQuery("SELECT MAX(no) FROM events");
		rs1.next();
		int max = rs1.getInt(1);
		counter = max+1;
	}
	else
		counter = 1;
	
	String query1= "INSERT INTO events values ("+counter + ",'"+events[0]+"','"+events[1]+"','"+events[2]+"','"+events[3]+"','project')";
	statement.executeUpdate(query1);
		connection.close();	
	}catch(ClassNotFoundException cnfe)
	{
		out.println("<br>Couldn't find class file" + cnfe); 
	}catch(SQLException sqle)
	{
		out.println("<br>SQL Exception: " + sqle);
	}catch(InstantiationException ie)
	{
		out.println("<br>Instant exception : " + ie);
	}catch(IllegalAccessException iae)
	{
		out.println("<br>Illegal access exception : " + iae);
	}
	response.sendRedirect("index.jsp");
}
else {
	response.sendRedirect("error.jsp");
}

%>